// ============================================================================
//  _ __   ___  ___  ___ _ __ ___  _   _
// | '_ \ / _ \/ __|/ _ \ '_ ` _ \| | | |
// | | | |  __/\__ \  __/ | | | | | |_| |
// |_| |_|\___||___/\___|_| |_| |_|\__,_|
//
// NES Emulator by daxnet, 2024
// MIT License
// ============================================================================

using NesEmu.Core;

namespace NesEmu.EmuConsoleNet;

public partial class FrmMain : Form
{
    #region Public Constructors

    public FrmMain()
    {
        InitializeComponent();
    }

    #endregion Public Constructors

    #region Private Methods

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void FrmMain_KeyDown(object sender, KeyEventArgs e)
    {
        SetControllerButton(true, e);
    }

    private void FrmMain_KeyUp(object sender, KeyEventArgs e)
    {
        SetControllerButton(false, e);
    }

    private void mnuClose_Click(object sender, EventArgs e)
    {
        gameControl.Stop();
    }

    private void mnuOpen_Click(object sender, EventArgs e)
    {
        if (openNesFileDlg.ShowDialog(this) == DialogResult.OK)
        {
            gameControl.LoadFromFile(openNesFileDlg.FileName);
            gameControl.Start();
        }
    }

    private void SetControllerButton(bool state, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.K:
                gameControl.SetButtonState(Controller.Buttons.A, state);
                break;

            case Keys.J:
                gameControl.SetButtonState(Controller.Buttons.B, state);
                break;

            case Keys.A:
                gameControl.SetButtonState(Controller.Buttons.Left, state);
                break;

            case Keys.D:
                gameControl.SetButtonState(Controller.Buttons.Right, state);
                break;

            case Keys.W:
                gameControl.SetButtonState(Controller.Buttons.Up, state);
                break;

            case Keys.S:
                gameControl.SetButtonState(Controller.Buttons.Down, state);
                break;

            case Keys.N:
                gameControl.SetButtonState(Controller.Buttons.Start, state);
                break;

            case Keys.B:
                gameControl.SetButtonState(Controller.Buttons.Select, state);
                break;
        }
    }

    #endregion Private Methods
}